// @ts-nocheck
const express = require('express');
const pool = require('../pool.js');

const router = express.Router();


// 房间列表整体查询(分页查询)
router.get('/lists', (req, res, next) => {
  const page_num = parseInt(req.query.page_num) || 1;
  const page_size = parseInt(req.query.page_size) || 10;
  const params = [(page_num - 1) * page_size, page_size];
  var sql = "select * from room limit ?,?";

  pool.query(sql, params, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length) {
      let sqlTotal = "select count(*) as total from room" //统计总数据条数命名为total
      pool.query(sqlTotal, (err, among) => {
        if (err) {
          console.log(err);
        } else {
          let total = among[0]['total'];
          res.json({
            "code": 1,
            "message": "查询成功",
            "data": data,
            "paging": {
              page_num: page_num,
              page_size: page_size,
              total: total
            }
          })
        }
      })
    } else {
      res.json({
        code: 0,
        message: '查询失败'
      })
    }
  })
})

// 房间列表按酒店名称编号查询
router.get('/:hotelname',(req,res,next)=>{
  let obj = req.params;
  pool.query('select * from room where hotelname=?',[obj.hotelname],(err,data)=>{
    if(err){next(err);return;}
    if (data.length) {
      res.send({
        "code":1,
        "msg":"加载成功",
        "data":data
      })
    } else {
      res.send({
        "code":0,
        "msg":"加载失败"        
      })
    }
  })
})


// 房间列表按酒店名称编号查询
// router.get('/:hotel_id', (req, res, next) => {
//   let obj = req.params;
//   pool.query('select * from room where hotel_id=?', [obj.hotel_id], (err, data) => {
//     if (err) {
//       next(err);
//       return;
//     }
//     if (data.length) {
//       res.send({
//         "code": 1,
//         "msg": "加载成功",
//         "data": data
//       })
//     } else {
//       res.send({
//         "code": 0,
//         "msg": "加载失败"
//       })
//     }
//   })
// })


// 查询某个酒店的某个套餐
router.get('/:hotel_id/:type', (req, res, next) => {
  let obj = req.params;
  pool.query('select * from room where hotel_id=? and type=?', [obj.hotel_id, obj.type], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    if (data.length) {
      res.send({
        "code": 1,
        "msg": "加载成功",
        "data": data
      })
    } else {
      res.send({
        "code": 0,
        "msg": "加载失败"
      })
    }
  })
})
//.修改房间资料
router.put('/edit',(req,res,next)=>{
  let obj=req.body;
  console.log(obj,"test");

  pool.query('update room set ? where rid=?',[obj,obj.rid],(err,result)=>{
    // pool.query(`update user set email="${obj.email}",phone="${obj.phone}",upwd="${obj.upwd}" where phone="${obj.oldphone}";`,(err,result)=>{
    console.log(result);
    if(err){next(err);return;}
	  //结果是对象，如果对象下的affectedRows值为0说明修改失败，否则修改成功
    if(result.affectedRows===0){
      res.send({code:0,msg:'修改失败'});
    }else{
      res.send({code:1,msg:'修改成功'});
    }
  });
});

//导出路由器对象
module.exports = router;